﻿using System;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.Collections.Generic;

namespace Eugenhome.Atrium.Mail
{
    /// <summary>
    /// 
    /// </summary>
    public class GmailFeed
    {
        /// <summary></summary>
        public XmlDocument Document = new XmlDocument();
        /// <summary></summary>
        private string _user;
        /// <summary></summary>
        private string _pass;
        /// <summary></summary>
        private string _proxyAddress;

        /// <summary></summary>
        public List<string> Errors = new List<string>();

        /// <summary>
        /// 
        /// </summary>
        private class IgnoreBadCerts : ICertificatePolicy
        {
            public bool CheckValidationResult(ServicePoint sp, X509Certificate certificate, WebRequest request, int error) { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pass"></param>
        /// <param name="proxyAddress"></param>
        public GmailFeed(string user, string pass, string proxyAddress)
        {
            _user = user;
            _pass = pass;
            _proxyAddress = proxyAddress;
        }

        /// <summary>
        /// 
        /// </summary>
        public void DataBind()
        {
            WebRequest req = WebRequest.Create("https://mail.google.com/mail/feed/atom");
            try
            {
                //ServicePointManager.CertificatePolicy = new IgnoreBadCerts();

                NetworkCredential cred = new NetworkCredential();
                cred.UserName = _user;
                cred.Password = _pass;
                req.Credentials = cred;
            }
            catch (Exception ex)
            {
                Errors.Add("NetworkCredential: " + ex.Message);
            }

            try
            {
                if (!_proxyAddress.Trim().Equals(string.Empty))
                {
                    req.Proxy = new WebProxy(_proxyAddress);
                }

                Stream resp = req.GetResponse().GetResponseStream();

                XmlReader reader = XmlReader.Create(resp);
                Document.Load(reader);
            }
            catch (Exception ex)
            {
                Errors.Add("Proxy=[" + _proxyAddress + "]: " + ex.Message);
            }
        }

        //public int Do2()
        //{
        //    XmlUrlResolver resolver = new XmlUrlResolver();
        //    resolver.Credentials = new NetworkCredential(_user, _pass);
        //    XmlReaderSettings settings = new XmlReaderSettings();
        //    settings.XmlResolver = resolver;
        //    try
        //    {
        //        XmlReader reader = XmlReader.Create("https://mail.google.com/mail/feed/atom", settings);
        //        doc.Load(reader);
        //    }
        //    catch (Exception a)
        //    {
        //        return -1;
        //    }
        //    return -1;

        //}
    }
}